package com.botao.tip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.botao.tip.dao.PurchaseMapper;
import com.botao.tip.entity.Purchase;
import com.botao.tip.service.PurchaseService;
import com.botao.tip.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : vip865047755@126.com
 * @version :JDK1.8
 * @date : 2020-12-03 12:54
 * @File : PurchaseServiceImpl.java
 * @software: IntelliJ IDEA
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Override
    public Boolean updatePurchase(String id, String title, String product, String supplier, String tel, String signingTime, String state, String manager) {
        try {
            Purchase purchase = purchaseMapper.selectOne(new QueryWrapper<Purchase>().eq("id", Integer.parseInt(id)));
            if (purchase == null) {
                return false;
            }
            purchase.setTitle(title);
            purchase.setProduct(product);
            purchase.setSupplier(supplier);
            purchase.setTel(tel);
            purchase.setSigningTime(signingTime);
            purchase.setState(Integer.parseInt(state));
            purchase.setManager(manager);
            return purchaseMapper.updateById(purchase) != 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Boolean insert(String title, String product, String supplier, String tel, String signingTime, String state, String manager) {
        try {
            Purchase purchase = new Purchase();
            purchase.setTitle(title);
            purchase.setProduct(product);
            purchase.setSupplier(supplier);
            purchase.setTel(tel);
            purchase.setSigningTime(signingTime);
            purchase.setState(Integer.parseInt(state));
            purchase.setManager(manager);
            return purchaseMapper.insert(purchase) != 0;
        } catch (Exception e) {
            return false;
        }
    }
}
